package Model;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class ReadFromURL {
	String temp = null;
	private static String convertStreamToString(InputStream is) throws UnsupportedEncodingException {
		 
	      BufferedReader reader = new BufferedReader(new    
	                              InputStreamReader(is, "UTF-8"));
	        StringBuilder sb = new StringBuilder();
	         String line = null;
	         try {
	                while ((line = reader.readLine()) != null) {
	                        sb.append(line + "n");
	                }
	           } catch (IOException e) {
	                e.printStackTrace();
	           } finally {
	                try {
	                        is.close();
	                } catch (IOException e) {
	                        e.printStackTrace();
	                }
	            }
	        return sb.toString();
	  }
	
	public String getContentFromURL(final URL url) {
		Runnable run = new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					URLConnection connection = url.openConnection();
					temp = ReadFromURL.convertStreamToString(connection.getInputStream());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		Thread t = new Thread(run);
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
	}
}
